//
//  CXHTopOnBannerEvent.m
//  CXHAdSDK-TopOn
//
//  Created by 陈坤 on 2024/10/10.
//

#import "CXHTopOnBannerEvent.h"
#import "CXHTopOnCommonBiddingRequest.h"
#import "CXHTopOnCommonBiddingManager.h"

@implementation CXHTopOnBannerEvent

/**
 广告获取成功
 
 @param bannerView banner实例
 */
- (void)CXH_bannerViewDidReceived:(CXHAdSDKBannerAdView *)bannerView{
    NSInteger price = bannerView.getBidPrice;
    if (self.isC2SBiding) {
        CXHTopOnCommonBiddingRequest *request = [[CXHTopOnCommonBiddingManager sharedInstance] getRequestItemWithUnitID:self.UUID];
        ATBidInfo *bidInfo = [ATBidInfo bidInfoC2SWithPlacementID:request.placementID
                                                  unitGroupUnitID:request.unitGroup.unitID
                                               adapterClassString:request.unitGroup.adapterClassString
                                                            price:@(price/100.0f).stringValue
                                                     currencyType:ATBiddingCurrencyTypeCNY
                                               expirationInterval:request.unitGroup.bidTokenTime
                                                     customObject:bannerView];
        bidInfo.networkFirmID = request.unitGroup.networkFirmID;
        
        if (request.bidCompletion) {
            request.bidCompletion(bidInfo, nil);
        }
    } else {
        [self trackBannerAdLoaded:bannerView adExtra:nil];
    }
}

/**
 广告拉取失败
 
 @param bannerView banner实例
 @param error 错误描述
 */
- (void)CXH_bannerViewFailToReceived:(CXHAdSDKBannerAdView *)bannerView error:(NSError *)error{
    if (self.isC2SBiding) {
        CXHTopOnCommonBiddingRequest *request = [[CXHTopOnCommonBiddingManager sharedInstance] getRequestItemWithUnitID:self.UUID];
        if (request.bidCompletion) {
            request.bidCompletion(nil, error);
        }
        [[CXHTopOnCommonBiddingManager sharedInstance] removeRequestItemWithUnitID:self.UUID];
    } else {
        [self trackBannerAdLoadFailed:error];
    }
}

/**
 广告点击
 
 @param bannerView 广告实例
 @param loadingPageURL 广告落地页地址，当渠道为bwt，并且customLoadingPage为YES时有值
 */
- (void)CXH_bannerViewClicked:(CXHAdSDKBannerAdView *)bannerView loadingPageURL:(NSString *)loadingPageURL{
    [self trackBannerAdClick];
}

/**
 广告点击上报
 
 @param bannerView 广告实例
 */
- (void)CXH_bannerViewClickedReport:(CXHAdSDKBannerAdView *)bannerView{
    
}

/**
 广告关闭
 
 @param bannerView 广告实例
 */
- (void)CXH_bannerViewClose:(CXHAdSDKBannerAdView *)bannerView{
    [self trackBannerAdClosed];
    
//    [[CXHTopOnCommonBiddingManager sharedInstance] removeRequestItemWithUnitID:self.UUID];
}

/**
 广告展示
 
 @param bannerView 广告实例
 */
- (void)CXH_bannerViewExposure:(CXHAdSDKBannerAdView *)bannerView{
    [self trackBannerAdImpression];
}

/**
 广告展示上报
 
 @param bannerView 广告实例
 */
- (void)CXH_bannerViewExposureReport:(CXHAdSDKBannerAdView *)bannerView{
    
}

/**
 关闭落地页
 
 @param bannerView 广告实例
 */
- (void)CXH_bannerAdCloseLandingPage:(CXHAdSDKBannerAdView *)bannerView{
    [self trackBannerAdDetailClosed];
}

- (void)dealloc{
    NSLog(@"------> %s", __FUNCTION__);
}

@end
